package calao.client.ui;

public abstract class SimpleFormat
extends Format
{

	final String pattern;

	public SimpleFormat(String pattern)
	{
		this.pattern = pattern;
	}
	
	abstract public String getFieldText(Object object, char fieldSymbol);

	public String formatAsText(Object object)
	{
		int state = 0;
		StringBuffer result = new StringBuffer();
		for (int i=0; i<pattern.length(); i++) {
			char c = pattern.charAt(i);
			if (state==0) {
				if (c=='%') state = 1;
				else result.append(c);
			}
			else {
				if (c=='%') result.append(c);
				else {
					String text = getFieldText(object, c);
					result.append(text!=null? text : "?");
				}
				state = 0;
			}
		}
		return result.toString();		
	}


}
